package com.liaoban.app.ui.mine;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.liaoban.app.R;
import com.liaoban.app.api.type.request.SettingInfoRequest;
import com.liaoban.app.biz.CommonBiz;
import com.liaoban.app.constants.ConfigKey;
import com.liaoban.app.constants.ExtraParam;
import com.liaoban.app.helper.ConfigHelper;
import com.liaoban.app.helper.ToastHelper;
import com.liaoban.app.ui.base.BaseToolbarActivity;
import com.liaoban.app.util.VerifyUtil;
import com.liaoban.app.widget.MyTextWatcher;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by ybc on 2018/7/10 0010.
 */
public class IntroduceActivity extends BaseToolbarActivity {
    @BindView(R.id.iv_right)
    ImageView ivRight;
    @BindView(R.id.et_content)
    EditText etContent;
    @BindView(R.id.tv_count)
    TextView tvCount;

    public static Intent newIntent(Context context, String content) {
        Intent intent = new Intent(context, IntroduceActivity.class);
        intent.putExtra(ExtraParam.CONTENT, content);
        return intent;
    }

    @Override
    protected String getNewTitle() {
        return "介绍";
    }

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_introduce;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        ivRight.setImageResource(R.mipmap.ic_confirm);
        ivRight.setVisibility(View.VISIBLE);
        String desc = ConfigHelper.getString(this, ConfigKey.USER_DESC);
        etContent.setText(desc);
        MyTextWatcher myTextWatcher = new MyTextWatcher(() -> {
            int count = etContent.getText().toString().trim().length();
            tvCount.setText(count + "/70");
        });
        etContent.addTextChangedListener(myTextWatcher);
    }

    @OnClick(R.id.iv_right)
    public void right() {
        String desc = etContent.getText().toString().trim();
        if(TextUtils.isEmpty(desc)) {
            ToastHelper.show(IntroduceActivity.this, "介绍内容不能为空");
            return;
        }
        String name = ConfigHelper.getString(this, ConfigKey.USER_NAME);
        String age = ConfigHelper.getString(this, ConfigKey.USER_AGE);
        String sex = ConfigHelper.getString(this, ConfigKey.USER_SEX);
        SettingInfoRequest settingInfoRequest = new SettingInfoRequest();
        settingInfoRequest.setWuserName(name);
        settingInfoRequest.setAge(age);
        settingInfoRequest.setDescription(desc);
        settingInfoRequest.setWuserSex(sex);
        showRunningDialog(R.string.committing);
        startTask(CommonBiz.getInstance().settingInfoSubmit(settingInfoRequest), baseResponse -> {
            ToastHelper.show(IntroduceActivity.this, baseResponse.msg);
            ConfigHelper.save(this, ConfigKey.USER_DESC, desc);
            finish();
        });
    }

}
